package com.example.mobile2021606129ly0420;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rvRecyclerView;
    private HomeAdapter mAdapter;
    private String[]words={"车身","底盘","车门","发动机","方向盘","后玻璃","轮胎","前玻璃","前盖","悬挂","座椅"};
    private int[] icons={R.drawable.ui_body01,R.drawable.ui_dipan01,R.drawable.ui_door01,R.drawable.ui_fadongji01,R.drawable.ui_fangxiangpan01,
            R.drawable.ui_houboli01,R.drawable.ui_luntai01,R.drawable.ui_qianboli01,R.drawable.ui_qiangai01,R.drawable.ui_xuangua01,R.drawable.ui_zhuoyi01};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rvRecyclerView = (RecyclerView) findViewById(R.id.rv_RecyclerView);
        rvRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new HomeAdapter();
        rvRecyclerView.setAdapter(mAdapter);
    }

     class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
         @Override
         public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
             MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.recycler, parent, false));
             return holder;
         }

         @Override
         public void onBindViewHolder(MyViewHolder holder, int position) {
             holder.word.setText(words[position]);
             holder.img.setImageResource(icons[position]);
         }

         @Override
         public int getItemCount() {
             return words.length;
         }

         class MyViewHolder extends RecyclerView.ViewHolder {
             TextView word;
             ImageView img;
             public MyViewHolder(View view){
             super(view);
             word =(TextView)
             findViewById(R.id.word);
             img =(ImageView)
             findViewById(R.id.img);
         }
     }
}
}
